#
# fusion.http.Collections
#
# Copyright 2007 Helsinki Institute for Information Technology
# and the authors.
#
# Authors: Ken Rimey <rimey@hiit.fi>
#          Toni Ruottu <toni.ruottu@iki.fi>
#

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

'''
Handle URLs of the form "/collections/".

Without parameters, GET displays a clickable list of the locally
available collections.
'''

import web

from fusion.http.util import (
    withdb, format_xml, read_form,
    authorized_referer, unauthorized)
from fusion.http.server import MAIN

class Collections(object):

    @withdb
    def GET(self, db):
        collections = db.list_collections()

        web.header('Content-Type', 'application/xhtml+xml; charset=UTF-8')

        print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
        print '    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
        print '<html xmlns="http://www.w3.org/1999/xhtml">'
        print '<head><title>Collections</title>'
        print '<style type="text/css">'
        print '''

div
{
    padding: 1em;
}

td
{
border-width: 0 0 thin 0;
border-style: solid;
border-color: #ccc;
padding: 0 0.2em;
}

.size
{
    font-size: 0.8em;
    text-align: right;
}

'''
        print '</style>'
        print '</head>'
        print '<body>'

        print '<em>' + format_xml('a', 'admin', href='/') + '</em>'
        print '/'
        print '<em>' + format_xml('a', 'collections', href='/collections/') + '</em>'

        print '<hr />'
        print '<div>'
        if not collections:
            print '<em>No collections</em>'
        else:
            print '<form action="/collections/" method="post">'
            print '<table>'
            for c in collections:
                print '<tr>'
                info = dict(db.query_text(c, 'info'))
                title = info.get('title') or c
                type = info.get('type')
                db_url = MAIN + '/collection/' + c + '/'
                app_url = db_url + 'web/index.html'
                type_name = ''
                if type:
                    type_name = type.split(':')[-1].replace('_', ' ')

                print '<td>'
                print format_xml('input', type='checkbox', name='checked', value=c)
                print '</td>'
                print '<td>'
                
                if type_name == 'island':
                    print format_xml('img', src='/static/icons/fusion_island.png', border='0', title=type_name)
                elif type_name == 'coast':
                    print format_xml('img', src='/static/icons/fusion_coast.png', border='0', title=type_name)
                elif type_name == 'chat':
                    print format_xml('img', src='/static/icons/fusion_chat.png', border='0', title=type_name)
                elif type_name == 'profile':
                    print format_xml('img', src='/static/icons/fusion_profile.png', border='0', title=type_name)
                elif type_name == 'photo album':
                    print format_xml('img', src='/static/icons/fusion_album.png', border='0', title=type_name)
                elif type_name == 'blog':
                    print format_xml('img', src='/static/icons/fusion_blog.png', border='0', title=type_name)
                elif len(db.keys(c)) < 1:
                    print format_xml('img', src='/static/icons/fusion_empty.png', border='0', title=type_name)
                elif type_name == '':
                    print format_xml('img', src='/static/icons/unknown_coltype.png', border='0', title=type_name)
                else:
                    print format_xml('img', src='/static/icons/missing_icon.png', border='0', title=type_name)
                print '</td>'
                print '<td>'

                if '' in db.keys(c,'/web/index.html'):
                    print format_xml('a', title, href=app_url)
                else:
                    print title

                print '</td>'
                print '<td class="size">'
                print len(db.keys(c))
                print ' items'
                print '</td>'
                print '<td>'
                        
                img = format_xml('img', src='/static/icons/folder.gif', border='0')
                print '<a href="%s">%s</a>' % (db_url, img)
                if type_name == 'island':
                    img = format_xml('img', src='/static/icons/feed-icon-14x14.png', border='0')
                    feed_url = MAIN + '/island/' + c + '/feed'
                    print '<a href="%s">%s</a>' % (feed_url, img)
                if type_name == 'coast':
                    img = format_xml('img', src='/static/icons/feed-icon-14x14.png', border='0')
                    feed_url = MAIN + '/coast/' + c + '/feed'
                    print '<a href="%s">%s</a>' % (feed_url, img)
                if type_name == 'blog':
                    img = format_xml('img', src='/static/icons/feed-icon-14x14.png', border='0')
                    feed_url = MAIN + '/feed/' + c
                    print '<a href="%s">%s</a>' % (feed_url, img)
                print '</td>'
                print '</tr>'
            print '</table>'
            print '<p>'
            print '<input type="submit" name="delete" value="Delete Selected" />'
            print '<input type="reset" value="Unselect All" />'
            print '</p>'
            print '</form>'
        print '</div>'

        print '</body>'
        print '</html>'

    @withdb
    def POST(self, db):
        if not authorized_referer():
            return unauthorized()

        form = read_form()
        if 'delete' in form:
            checked = form.getlist('checked')
            for c in checked:
                db.remove_collection(c)

            if db.get_local_info('default_profile') in checked:
                db.set_local_info('default_profile', None)

        return web.seeother('/collections/')
